package tdam.androide.web;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import android.content.Context;

public class Solicitud {

	private static String requestRegistarUsuarioWeb;
	private static String requestEnviarMensajeWeb;
	private static String requestConsultarMensajesWeb;
	
/*	private static final String TAG_ACTION = "action";
	private static final String TAG_ACTION_DETAIL = "action-detail";
	private static final String TAG_USER = "user";
	private static final String TAG_RESULT = "result";
	private static final String TAG_DETAIL = "detail";
	private static final String TAG_AUTH = "auth";
	private static final String TAG_FILTER = "filter";
	private static final String TAG_MESSAGE_LIST = "filter";
	private static final String TAG_MESSAGE = "message";
	
	private static final String ATT_ID = "id";
	private static final String ATT_ACTION_NAME = "name";
	private static final String ATT_USERNAME = "username";
	private static final String ATT_PASSWORD = "password";
	private static final String ATT_KEY = "key";
	private static final String ATT_TYPE = "type";
	private static final String ATT_CODE = "code";
	private static final String ATT_DESCRIPTION = "description";
	private static final String ATT_TIMESTAMP = "timestamp";
	private static final String ATT_FROM = "from"; 	
	private static final String ATT_TO = "to"; 	
	
	private static final String REQUEST_RANDOM_VALUE = "REQUEST_RANDOM_VALUE"; 	
	private static final String REGISTER_USER = "register-user";
	private static final String SEND_MESSAGE = "send-message";
	private static final String SEND_MESSAGE = "get-messages";
*/	
	public Solicitud () {}
	
	/**
	*
	*Este metodo hace ...
	*@param ...
	*@return ...
	*@exception ...
	*@throws ...
	*
	*/
	
	public static String registrarUsuarioWeb(Context contexto, String REQUESTED_USERNAME, String USER_PASSWORD) {
		requestRegistarUsuarioWeb = "";
		try {
			requestRegistarUsuarioWeb = 	"<action id=\"REQUEST_RANDOM_VALUE\" name=\"register-user\">" +
												"<action-detail>" +
													"<user username=\"" + REQUESTED_USERNAME + "\" password=\"" + USER_PASSWORD + "\"/>" +
												"</action-detail>" +
											"</action>";
		
			FileOutputStream archivo = contexto.getApplicationContext().openFileOutput("register-user.xml", Context.MODE_PRIVATE);
			OutputStreamWriter fout = new OutputStreamWriter(archivo);
			fout.write(requestRegistarUsuarioWeb);
			fout.close();
		} catch (Exception e) {}
		return requestRegistarUsuarioWeb;
	}
	
	/**
	*
	*Este metodo hace ...
	*@param ...
	*@return ...
	*@exception ...
	*@throws ...
	*
	*/
	
	public static String enviarMensajeWeb(Context contexto, String USERNAME, String USER_KEY, String RECIPIENT, String MESSAGE) {
		requestEnviarMensajeWeb = "";
		try {
			requestEnviarMensajeWeb = 		"<action id=\"REQUEST_RANDOM_VALUE\" name=\"send-message\">" +
												"<action-detail>" +
													"<auth username=\"" + USERNAME + "\" key=\"" + USER_KEY + "\"></auth>" +
													"<message to=\"" + RECIPIENT + "\"><![CDATA[" + MESSAGE + "]]></message>" +
												"</action-detail>" +
											"</action>";
		
			FileOutputStream archivo = contexto.getApplicationContext().openFileOutput("send-message.xml", Context.MODE_PRIVATE);
			OutputStreamWriter fout = new OutputStreamWriter(archivo);
			fout.write(requestEnviarMensajeWeb);
			fout.close();
		} catch (Exception e) {}
		return requestEnviarMensajeWeb;
	}
	
	/**
	*
	*Este metodo hace ...
	*@param ...
	*@return ...
	*@exception ...
	*@throws ...
	*
	*/
	
	public static String consultarMensajesWeb(Context contexto, String USERNAME, String USER_KEY, String TIMESTAMP) {
		requestConsultarMensajesWeb = "";
		try {
			requestConsultarMensajesWeb = 	"<action id=\"REQUEST_RANDOM_VALUE\" name=\"get-messages\">" +
												"<action-detail>" +
													"<auth username=\"" + USERNAME + "\" key=\"" + USER_KEY + "\"></auth>" +
													"<filter type=\"timestamp\">" + TIMESTAMP + "</filter>" +
												"</action-detail>" +
											"</action>";
		
			FileOutputStream archivo = contexto.getApplicationContext().openFileOutput("get-messages.xml", Context.MODE_PRIVATE);
			OutputStreamWriter fout = new OutputStreamWriter(archivo);
			fout.write(requestConsultarMensajesWeb);
			fout.close();
		} catch (Exception e) {}
		return requestConsultarMensajesWeb;
	}

/*	public static boolean  prepareSendMessage(String usrName, String usrPassword,  MensajeWeb message, String fileRequest){
		
		File newxmlfile = new File(fileRequest);
		try {
			newxmlfile.createNewFile();
		
		FileOutputStream fileos = null;
	    fileos = new FileOutputStream(newxmlfile);
	
		XmlSerializer serializer = Xml.newSerializer();
		
		  serializer.setOutput(fileos, "UTF-8");

		  serializer.startDocument(null, Boolean.valueOf(true));
		  serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
		  
		  serializer.startTag("", TAG_ACTION);
		  serializer.attribute("", ATT_ID, REQUEST_RANDOM_VALUE);
		  serializer.attribute("", ATT_ACTION_NAME, SEND_MESSAGE);
		  serializer.startTag("", TAG_ACTION_DETAIL);
		  serializer.startTag("", TAG_AUTH);
		  serializer.attribute("", ATT_USERNAME, usrName);
		  serializer.attribute("", ATT_KEY, usrPassword);
		  serializer.endTag("", TAG_AUTH);
		  serializer.startTag("", TAG_MESSAGE);
		  serializer.attribute("", ATT_TO, message.getUsuario());
		  serializer.text(message.getMensaje());
		  serializer.endTag("", TAG_MESSAGE);
		  serializer.endTag("", TAG_ACTION_DETAIL);
		  serializer.endTag("", TAG_ACTION);
		  
		  serializer.endDocument();
		  serializer.flush();
		  fileos.close();
		  
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public static boolean prepareXmlRegisterUserWeb(String usrName, String usrPassword, String file){
		
		File newxmlfile = new File(file);
		try {
			newxmlfile.createNewFile();
		
	
		FileOutputStream fileos = null;
	    fileos = new FileOutputStream(newxmlfile);
	
		XmlSerializer serializer = Xml.newSerializer();

		  serializer.setOutput(fileos, "UTF-8");
		  serializer.startDocument(null, Boolean.valueOf(true));
		  serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
		  
		  serializer.startTag("", TAG_ACTION);
		  serializer.attribute("", ATT_ID, REQUEST_RANDOM_VALUE);
		  serializer.attribute("", ATT_ACTION_NAME, REGISTER_USER);
		  serializer.startTag("", TAG_ACTION_DETAIL);
		  serializer.startTag("", TAG_USER);
		  serializer.attribute("", ATT_USERNAME, usrName);
		  serializer.attribute("", ATT_PASSWORD, usrPassword);
		  serializer.endTag("", ATT_USERNAME);
		  serializer.endTag("", TAG_ACTION_DETAIL);
		  serializer.endTag("", TAG_ACTION);
		  
		  serializer.endDocument();
		  serializer.flush();
		  fileos.close();
		  
		  return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
*/	
}
